#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""外星人入侵地球"""
import pygame
from settings import Settings  # settings.py 这个模块, 从 settings 模块里面导入其中的 Settings 类的定义
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from game_stats import GameStats
from button import Button


def run_game():
    """初始化游戏并创建一个屏幕对象"""
    pygame.init()
    ai_settings = Settings()
    stats = GameStats(ai_settings)
    # 实例化 Settings成为一个对象叫 ai_settings
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height), 0, 32)
    ship = Ship(ai_settings, screen)
    play_button = Button(ai_settings, screen, "PLAY")
    # 创建一个存放子弹的编组
    bullets = Group()
    aliens = Group()
    # 创建外星人群
    gf.create_fleet(ai_settings, screen, ship, aliens)
    pygame.display.set_caption(ai_settings.caption)


    while True:
        gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets)# 检测事件
        if stats.game_active:
            ship.update()  # 更新飞船的位置
            gf.update_bullets(ai_settings, screen, ship, aliens, bullets)
            # 因为外星人一直动,我就要一直刷新外星人的位置
            gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets)
        gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button)  # 渲染飞船和子弹


run_game()
